package cn.itcast.item.service.impl;


import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;

import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    private ItemMapper itemMapper;



    /**
     * 分页查询商品
     *
     * @param searchItemDTO
     * @return
     */
    @Override
    public PageDTO<Item> selectPage(SearchItemDTO searchItemDTO) {
        IPage<Item> page = new Page<>(searchItemDTO.getPage(),searchItemDTO.getSize());
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(searchItemDTO.getName()),Item::getName,searchItemDTO.getName())
                .eq(StringUtils.isNotEmpty(searchItemDTO.getBrand()),Item::getBrand,searchItemDTO.getBrand())
                .between(Item::getCreateTime,searchItemDTO.getBeginDate(),searchItemDTO.getEndDate())
                .orderByAsc(Item::getCategory);
        IPage<Item> iPage = itemMapper.selectPage(page, wrapper);
        PageDTO<Item> item = new PageDTO<>();
        item.setTotal(iPage.getTotal());//当前页数的总条数
        item.setList(iPage.getRecords());//当前页的总数据
        return item;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public Item selectItemById(Long id) {
        if (id == null || "".equals(id)) {
            throw new RuntimeException("id不能为空");
        }
        Item item = itemMapper.selectById(id);
        return item;
    }

    /**
     * 新增数据
     *
     * @param item
     * @return
     */
    @Override
    public ResultDTO insertItem(Item item) {
        //判断请求参数是否为空
        if (item == null || "".equals(item)) {
            throw new RuntimeException("参数不能为空");
        }
        //获取当前修改时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = df.parse(df.format(new Date()));
            item.setCreateTime(date);
            item.setUpdateTime(date);
            int insert = itemMapper.insert(item);
            if (insert > 0) {
                return ResultDTO.ok();
            }
            return ResultDTO.error();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("错误");
        }

    }
    /**
     * 上架、下架
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResultDTO statusIdItem(Long id, Integer status) {
        //判断请求参数是否为空
        if((id==null||"".equals(id))&&(status==null||"".equals(status))){
            throw new RuntimeException("id或状态不能为空");
        }

        //根据id修改数据状态
            LambdaUpdateWrapper<Item> wrapper = Wrappers.<Item>lambdaUpdate();
        //获取当前修改时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(System.currentTimeMillis());
            wrapper.eq(Item::getId,id)
                    .set(Item::getUpdateTime,format)
                    .set(Item::getStatus,status);
        int count = itemMapper.update(null, wrapper);
        //判断是否修改成功 count>0成功
        if(count>0){
            //返回修改成功信息
            return ResultDTO.ok();
        }
        //返回失败信息
        return ResultDTO.error();
    }

    /**
     * 修改商品信息
     * @param item
     * @return
     */
    @Override
    public ResultDTO updateStatus(Item item) {
        if(item==null || "".equals(item)){
            throw new RuntimeException("参数不能为空");
        }
        LambdaUpdateWrapper<Item> wrapper = Wrappers.lambdaUpdate();
        //获取当前修改时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = df.parse(df.format(new Date()));
            wrapper.eq(Item::getId,item.getId())
                    .set(Item::getName,item.getName())
                    .set(Item::getCategory,item.getCategory())
                    .set(Item::getBrand,item.getBrand())
                    .set(Item::getPrice,item.getPrice())
                    .set(Item::getStock,item.getStock())
                    .set(Item::getSpec,item.getSpec())
                    .set(Item::getImage,item.getImage())
                    .set(Item::getIsAD,item.getIsAD())
                    .set(Item::getUpdateTime,date);
            int count = itemMapper.update(null,wrapper);
            //判断是否修改成功 count>0成功
            if(count>0){
                //返回修改成功信息
                return ResultDTO.ok();
            }
            //返回失败信息
            return ResultDTO.error();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException("获取时间异常");
        }

    }

    /**
     * 根据id删除商品信息
     * @param id
     * @return
     */
    @Override
    public ResultDTO deleteById(Long id) {
        if(id==null||"".equals(id)){
            throw new RuntimeException("id不能为空");
        }
        int count = itemMapper.deleteById(id);
        if(count>0){
            //返回修改成功信息
            return ResultDTO.ok();
        }
        //返回失败信息
        return ResultDTO.error();
    }

    @Override
    public PageDTO<Item> selectAllPage(Integer i) {
        LambdaQueryWrapper<Item> wrapper = Wrappers.lambdaQuery(Item.class);
        //构建分页对象
            IPage<Item> page=new Page<>(i,100);
            IPage<Item> item = itemMapper.selectPage(page, wrapper);
        PageDTO<Item> pageitem = new PageDTO<>();
        pageitem.setList(item.getRecords());
        pageitem.setTotal(item.getTotal());
        return pageitem;
    }

}
